﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Bai2;

namespace cau2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private SinhVien sv = new SinhVien();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = sv.DanhSachSV();
            datagrid.ItemsSource = dt.DefaultView;
            datagrid.SelectedValuePath = "masv";
            txtmasv.Focus();

        }

        private void btnthem_Click(object sender, RoutedEventArgs e)
        {
            string masv = txtmasv.Text;
            string hosv = txthosv.Text;
            string tensv = txtten.Text;
            string ns = txtngsinh.Text;
            string gt = txtgt.Text;
            string mkhoa = txtmakhoa.Text;
            sv.ThemSV(masv, hosv, tensv, ns, gt, mkhoa);
            DataTable dt = sv.DanhSachSV();
            datagrid.ItemsSource = dt.DefaultView;
            datagrid.SelectedValuePath = "masv";
            datagrid.Items.Refresh();


        }

        private void btnsua_Click(object sender, RoutedEventArgs e)
        {
            DataRowView datarow = (DataRowView)datagrid.SelectedItem;

            txtmasv.Text = datarow["masv"].ToString();
            txthosv.Text = datarow["hosv"].ToString();
            txtten.Text = datarow["tensv"].ToString();
            txtngsinh.Text = datarow["ngaysinh"].ToString();
            txtgt.Text = datarow["gioitinh"].ToString();
            txtmakhoa.Text = datarow["makhoa"].ToString();
        }

        private void btnxoa_Click(object sender, RoutedEventArgs e)
        {

            MessageBoxResult result = MessageBox.Show("Bạn có muốn xóa không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                string masv = datagrid.SelectedValue.ToString();
                sv.XoaSV(masv);
                DataRowView rowSelect = (DataRowView)datagrid.SelectedItem;
                rowSelect.Delete();
            }

        }

        private void btnluu_Click(object sender, RoutedEventArgs e)
        {
            string masv = txtmasv.Text;
            string hosv = txthosv.Text;
            string tensv = txtten.Text;
            string ns = txtngsinh.Text;
            string gt = txtgt.Text;
            string mkhoa = txtmakhoa.Text;
            sv.SuaSV(masv, hosv, tensv, ns, gt, mkhoa);
            DataTable dt = sv.DanhSachSV();
            datagrid.ItemsSource = dt.DefaultView;
            datagrid.Items.Refresh();

        }

        private void btnhuy_Click(object sender, RoutedEventArgs e)
        {
            txtmasv.Text = null;
            txtten.Text = null;
            txthosv.Text = null;
            txtngsinh.Text = null;
            txtgt.Text = null;
            txtmakhoa.Text = null;
            txtmasv.Focus();
        }

        private void btnthoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void datagrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {

        }


    }
}
